//
//  HZHomeVM.m
//  微博 OC
//
//  Created by 黑眼圈 on 16/4/21.
//  Copyright © 2016年 heiyan. All rights reserved.
//

#import "HZHomeVM.h"
#import "HZOAuthVM.h"
#import "HZNetTools.h"
#import "HZHomeModel.h"
#import "HZHomeCellVM.h"

@implementation HZHomeVM



#pragma mark -
#pragma mark    获取网络的主页信息
-(void)giveHomeMess:(void(^)(NSArray* array))finished{
    
    
    NSString* urlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    
    NSString* token = [HZOAuthVM sharedOAuth].model.access_token;
    
    NSDictionary* paras = @{@"access_token":token};
    
    //主页的 get 请求
    [[HZNetTools sharedTools] requestWithMethod:HZRequestMethodGet urlString:urlString parameters:paras finishedBlock:^(NSDictionary* responstObject, NSError *error) {
        
        if (error != nil) {
            
            NSLog(@"请求主界面数据失败");
            NSLog(@"%@",error);
        }
        
        
        NSArray* arr = responstObject[@"statuses"];
        
        //定义可变数组来接收
        NSMutableArray* arrM = [NSMutableArray array];
        
        //遍历字典转模型
        for (NSDictionary* dict in arr) {
            
            HZHomeModel* model = [HZHomeModel homeWithDict:dict];
            
            //创建模型对应的 VM
            HZHomeCellVM* cellVM = [[HZHomeCellVM alloc]init];
            
            //给 cellVM赋值模型
            cellVM.model = model;
            
            //数组里全是 cellVM
            [arrM addObject: cellVM];
        }
        
        //成功的 blcok 回调
        finished(arrM.copy);
        

    }];
    
    
    
}



@end
